package edu.spsu.cs4253;



import java.net.*;
import java.io.*;
/*
 * Subclass of DatagramSocket which contains methods for sending and receiving messages
 * 
 */

public class SPSUSocket extends DatagramSocket {
	static final int MAX_LEN =1024;
	SPSUSocket(int portNo) throws SocketException{
		super (portNo);
	}
	
	/**
	 * Sends data and prints confirmation
	 * @param host
	 * @param port
	 * @param sendData
	 * @return void
	 * @throws Exception 
	 */
	public void sendMessage(InetAddress host, int port, byte[] sendData) throws Exception{
		DatagramPacket datagram =
				new DatagramPacket(sendData, sendData.length, host, port);
		String message = sendData.toString();
		System.out.println(" " + message + " will be sent.\n");
		UDPServer server = new UDPServer();
	}
	
	/**
	 * Receives data and prints confirmation
	 * @return DatagramPacket
	 * @throws IOException
	 */
	public DatagramPacket receiveMessage() throws IOException{
		byte[] receiveData = new byte[MAX_LEN];
		DatagramPacket datagram =
				new DatagramPacket(receiveData, MAX_LEN);
		this.receive(datagram);
		String message = new String (datagram.getData());
		System.out.println(" " + message + " has been received.\n");
		return datagram;
		
		}

}
